package zx.x.modules.app.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class BeanUtils {
    /**
     * 拷贝bean中的属性
     * @param source
     * @param target
     * @param ignores
     */
    public static void copyProperties(Object source, Object target, String... ignores){
        org.springframework.beans.BeanUtils.copyProperties(source, target, ignores);
    }
    /**
     * 拷贝bean中的属性 忽略掉空值属性和ID属性
     * @param source
     * @param target
     */
    public static void copyPropertiesIgnoreNullValueAndId(Object source, Object target){
        copyProperties(source, target, getNullPropertyNamesAndIgnoreNames(source, "id"));
    }
    /**
     * 拷贝bean中的属性 忽略掉空值属性
     * @param source
     * @param target
     */
    public static void copyPropertiesIgnoreNullValue(Object source, Object target){
        copyProperties(source, target, getNullPropertyNamesAndIgnoreNames(source));
    }
    /**
     * 获取空值属性名称或者忽略
     * @param source
     * @param ignores
     * @return
     */
    public static String[] getNullPropertyNamesAndIgnoreNames (Object source, String... ignores ) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        if (ignores != null && ignores.length > 0){
            emptyNames.addAll(Arrays.asList(ignores));
        }
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static String[] convertStringToArray(String ids){
        if (StringUtil.isEmpty(ids)){
            return null;
        }
        return ids.split(",");
    }
    /**
     * 分解URL协议、host、端口、path等参数
     * "http://www.baidu.com:8080/1223/sdfsd?abc=rer";
     * 	String protocol = "";
     String host = "";
     String port = "";
     String path = "";
     ["http","www.baidu.com","8080","/1223/sdfsd?abc=rer"]
     * @param url
     * @return
     */
    public static List<String> splitUrlPath(String url){
        if (StringUtil.isEmpty(url)){
            return null;
        }
        url = url.trim();
        String protocol = "";
        String host = "";
        String port = "";
        String path = "";

        List<String> params = new ArrayList<>();
        String[] protocols = url.split("://");
        if (protocols == null || protocols.length != 2){
            return null;
        }
        protocol = protocols[0];
        if (StringUtil.isEmpty(protocols[1])){
            return null;
        }
        if (protocols[1].indexOf(':') < 0){//没有端口号，即80端口
            port = "80";
            String[] hosts = protocols[1].split("/");
            if (hosts == null || hosts.length < 2){
                host = protocols[1];
                path = "";
            } else {
                host = hosts[0];
                path = protocols[1].substring(host.length(), protocols[1].length());
            }
        } else {
            String[] hosts = protocols[1].split(":");
            host = hosts[0];
            String[] ports = hosts[1].split("/");
            port = ports[0];
            path = hosts[1].substring(port.length(), hosts[1].length());
        }
        params.add(protocol);
        params.add(host);
        params.add(port);
        params.add(path);
        return params;
    }
}

